<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Account_Model extends CI_Model
{

	function test(){

		//$this->db->where('product_id', $product_id);

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();		

	}
	
	function update_user($user_id){
		$data = array(
			'username' => $this->input->post('username'),
			'first_name' => $this->input->post('first_name'),
			'last_name' =>  $this->input->post('last_name'),
			'email' =>  $this->input->post('email')
		);
        $this->db->where('id', $user_id);
        $this->db->update('users', $data);
	}
	
	function select($user_id){
        
		return $this->db->get_where('users', array('id'=>$user_id))->row();
    }
	
	function select2($user_id){
		return $this->db->query('SELECT * FROM customer, `order`, users where customer.user_id = users.id and customer.customer_id = `order`.customer_id and users.id = "'.$user_id.'"')->row();
	}
	
	function get_customer_by_user($user_id){
		return $this->db->query('SELECT * FROM customer, users where id = user_id and user_id = "'.$user_id.'"')->row();	
	}
	
	function get_order_by_customer($customer_id){
		return $this->db->get_where('order', array('customer_id'=>$customer_id))->row();
	}
	
	function get_order_by_user($user_id){
		return $this->db->query('SELECT * FROM `order` where customer_id in (select customer_id from customer where user_id = "'.$user_id.'")')->row();	
	}
	
	function update_address($user_id){
		$data = array(
					'address1' => $this->input->post('address1'),
					'address2' =>  $this->input->post('address2'),
					'city' =>  $this->input->post('city'),
					'post_code' => $this->input->post('post_code'),
					'country_id' =>  $this->input->post('country'),
					'region_id' =>  $this->input->post('region'),
					'telephone' => $this->input->post('telephone'),
					'date_change' =>  date ('Y-m-d H:i:s')
				);
		$this->db->where('user_id', $user_id);
        $this->db->update('customer', $data);
	}
	
	function get_country(){		
		$this->db->order_by('name ASC');
		$this->db->where('status', 1);
		$query = $this->db->get('country');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}
	
	function get_region_by_country_id($country_id){
		$this->db->where('country_id', $country_id);
		$this->db->where('status', 1);
		$this->db->order_by('name ASC');
		$query = $this->db->get('zone');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}
	
	function get_order_by_id($order_id){
        return $this->db->get_where('order', array('order_id'=>$order_id))->row();
	}
	
	function get_order_product_by_id($order_id){
		$this->db->where('order_id', $order_id);
		$query = $this->db->get('order_product');
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_total_by_id($order_id){
		$this->db->where('order_id', $order_id);
		$query = $this->db->get('order_total');
		
		if($query->num_rows > 0){
			foreach ($query->result() as $rows){
				$data[] = $rows;
			}
			return $data;
		}	
	}
	
	function sub_total_order_by_id($order_id){
		$query = $this->db->query('select sum(subtotal) as subtotal from order_product where order_id = "'.$order_id.'"');
		
		//echo $this->db->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function order_total_by_id_code($order_id, $code){
		return $query = $this->db->query('select sum(value) as value from order_total where order_id = "'.$order_id.'" and code = "'.$code.'" ')->row();
		
		//echo $this->db->last_query();
		
	}
	
	function get_history($order_id){
		
		$this->db->where('order_id', $order_id);
		
		$query = $this->db->get('order_history');
		
		//echo $this->db->last_query();
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}
	}
	
	function get_product($product_id){
		$this->db->where('product_id', $product_id);
		$query = $this->db->get('product');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}
	}
	
	function get_country_by_customer($country_id){
		return $this->db->get_where('country', array('country_id'=>$country_id))->row();
	}
	
	function get_region_by_customer($region_id){
		return $this->db->get_where('zone', array('zone_id'=>$region_id))->row();
	}
	
	function get_shipping_by_product($product_id){
		return $this->db->get_where('order_shipping', array('product_id'=>$product_id))->row();
	}
	
	function get_store($user_id){
		$this->db->where('user_id', $user_id);

		$query = $this->db->get('store');
		if ($query->num_rows() == 1) return $query->row();
		return NULL;

	}
	
	function create_address(){
		$data = array(
					'user_id' => Modules::run('auth/get_user_id'),
					'address1' => $this->input->post('address1'),
					'address2' =>  $this->input->post('address2'),
					'city' =>  $this->input->post('city'),
					'post_code' => $this->input->post('post_code'),
					'country_id' =>  $this->input->post('country'),
					'region_id' =>  $this->input->post('region'),
					'telephone' => $this->input->post('telephone'),
					'register_ip' => $_SERVER['REMOTE_ADDR'],
					'date_add' =>  date ('Y-m-d H:i:s')
				);
        $this->db->insert('customer', $data);
	}
	
}